<?php
/**
 * VgotFaster PHP Framework
 *
 * @package VgotFaster
 * @author pader
 * @copyright Copyright (c) 2009-2011, VGOT.NET
 * @link http://www.vgot.net/ http://vgotfaster.googlecode.com
 * @filesource
 */

/**
 * Controller Parent Class
 *
 * @package VgotFaster
 * @subpackage Interface
 * @author pader
 */
abstract class Controller extends VgotFaster {

	public static $afterRuntimeFunctions = array();

	public function Controller()
	{
		parent::_setInstance();

		//Load Default Libraries
		$systemLoads = array(
			'config',
			'load' => 'loader',
			'input'
		);

		foreach($systemLoads as $instance => $class) {
			is_numeric($instance) AND $instance = $class;
			$this->$instance =& loadClass($class);
		}

		//Autoload
		$this->load->autoload();
	}

	public function _overload()
	{
		error404();
	}

	/**
	 * To Run The Registered Functions
	 *
	 * @return void
	 */
	public function _ControllerAfterRuntime()
	{
		if(empty(self::$afterRuntimeFunctions)) return;

		foreach(self::$afterRuntimeFunctions as $library => $function) {
			if(is_numeric($library)) {
				call_user_func_array($function);
			} else {
				if(!is_array($function)) continue;

				$VF =& getInstance();

				printr($VF);

				foreach($function as $method) {
					$VF->$library->$method();
				}
			}
		}
	}

}

/**
 * Model Parent Class
 *
 * @package VgotFaster
 * @subpackage Interface
 * @author pader
 */
abstract class Model {

	protected $_selfClassName = '';

	public function Model()
	{
		$this->_selfClassName = ucfirst(get_class($this));
		$this->_assignLibraries();
	}

	//Assign Loaded Libraries To Model Self
	public function _assignLibraries($instance='')
	{
		$VF =& getInstance();
		foreach (array_keys(get_object_vars($VF)) as $key) {
			if (is_object($VF->$key) AND !($VF->$key instanceof $this->_selfClassName)) {
				//$instance AND $VF->$key->$instance =& $this;
				empty($this->$key) AND $this->$key =& $VF->$key;
			}
		}
	}

}

/**
 * Register A Function After Controller Run End
 *
 * @param string $callback Function or object name
 * @param string $method
 * @return void
 */
function registerControllerAfterFunction($callback,$method='') {
	if($method == '') {  //Function
		$skey = array_search($callback,Controller::$afterRuntimeFunctions);
		if($skey === FALSE or !is_numeric($skey)) {
			Controller::$afterRuntimeFunctions[] = $callback;
		}
	} else {  //Object method
		$VF =& getInstance();

		if(!isset(Controller::$afterRuntimeFunctions[$callback])) {
			Controller::$afterRuntimeFunctions[$callback] = array();
		}

		if(!in_array($method,Controller::$afterRuntimeFunctions[$callback])) {
			Controller::$afterRuntimeFunctions[$callback][] = $method;
		}
	}
}

//.